package com.brush.web.controller;

import com.brush.app.domain.req.question.QuestionAddReq;
import com.brush.app.domain.req.question.QuestionEditReq;
import com.brush.app.domain.req.question.QuestionReq;
import com.brush.app.domain.resp.question.QuestionDetailResp;
import com.brush.app.domain.resp.question.QuestionResp;
import com.brush.app.service.AppQuestionService;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 功能描述
 *
 * @author tonneyyy
 */
@Api(tags = "后台-试题管理")
@RestController
@RequestMapping(value = "/admin/question")
public class QuestionController extends BaseController {

    @Autowired
    private AppQuestionService appQuestionService;

    @PreAuthorize("@ss.hasPermi('app:question:list')")
    @ApiOperation(value = "试题分页查询")
    @GetMapping(value = "/list")
    public R<PageInfo<QuestionResp>> pagination(QuestionReq req){
        PageInfo<QuestionResp> pageInfo= appQuestionService.pagination(req);
        return R.ok(pageInfo);
    }

    @ApiOperation(value = "新增题目")
    @PreAuthorize("@ss.hasPermi('app:question:add')")
    @PostMapping
    public R add(@RequestBody @Valid QuestionAddReq req){
        boolean result=this.appQuestionService.add(req);
        return super.isSuccess(result);
    }

    @ApiOperation(value = "根据id查询题目")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('app:question:query')")
    public R<QuestionDetailResp> findById(@PathVariable(value = "id") Long id){
        if(Objects.isNull(id) || id.intValue()<=0){
            return R.fail("参数错误");
        }
        QuestionDetailResp resp= this.appQuestionService.findById(id);
        return R.ok(resp);
    }

    @ApiOperation(value = "修改题目")
    @PreAuthorize("@ss.hasPermi('app:question:edit')")
    @PutMapping
    public R edit(@RequestBody @Valid QuestionEditReq req){
        boolean result=this.appQuestionService.edit(req);
        return super.isSuccess(result);
    }

}
